﻿#include "PyFITKPhyscisPropGeneralLaminate.h"
#include "FITKAcousticsPhysics.h"
#include "FITKAcousticsAbsPhysicsProp.h"
#include "FITKPhyscisPropertyGeneralLaminate.h"
#include "FITKAcousticsAbsMaterial.h"
#include "FITKAcousticsSpectraManager.h"

namespace Acoustics {
    PyFITKPhyscisPropGeneralLaminate::PyFITKPhyscisPropGeneralLaminate(const QString & name) :
        Acoustics::PyFITKPhyscisPropBase(name)
    {
    }

    bool PyFITKPhyscisPropGeneralLaminate::addLayer(const QString & material, double thickness, double angle, const QString & DLFSpectra)
    {
        if (!_phyProp)return false;
        if (material == "" || thickness < 0)return false;
        FITKPhyscisPropertyGeneralLaminate *generalLaminate = dynamic_cast<Acoustics::FITKPhyscisPropertyGeneralLaminate*>(_phyProp);
        if (!generalLaminate)return false;

        //获取材料ID 并判断材料类型是否是ViscoElastic
        Acoustics::FITKAcousticsPhysics* phy = Acoustics::FITKAcousticsPhysics::GetAcousticsPhysicsFromFrame();
        if (phy == nullptr) return false;
        Acoustics::FITKAcousticsMaterialManager* matMgr = phy->getMaterialManager();
        if (matMgr == nullptr) return false;
        if (!matMgr->getDataByName(material))return false;
        int MaterialID = matMgr->getDataByName(material)->getDataObjectID();
        Acoustics::FITKAcousticsAbsMaterial* materialPtr = dynamic_cast<Acoustics::FITKAcousticsAbsMaterial*>(matMgr->getDataByName(material));
        bool isViscoElastic = materialPtr->getAcousticsMaterialType() == Acoustics::FITKAcousticsAbsMaterial::ViscoElastic;

        //获取频谱ID  
        Acoustics::FITKAcousticsSpectraManager* SpeMgr = phy->getFITKAcousticsSpectraManager();
        if (SpeMgr == nullptr) return false;
        int SplectrumID = -1;
        if (!SpeMgr->getDataByName(DLFSpectra)) {
            if (DLFSpectra != "")return false;
            if (!isViscoElastic)return false;
        }
        else {
            SplectrumID = SpeMgr->getDataByName(DLFSpectra)->getDataObjectID();
        }

        //创建新层
        FITKPhyscisPropertyGeneralLaminateIsoLayer* layer = new FITKPhyscisPropertyGeneralLaminateIsoLayer();
        layer->setMaterialID(MaterialID);
        layer->setThickness(thickness);
        layer->setAngle(angle);
        if (SplectrumID != -1)  layer->setDLFSplectrumID(SplectrumID);

        //追加层
        generalLaminate->appendDataObj(layer);
        return true;
    }

    bool PyFITKPhyscisPropGeneralLaminate::insertLayer(int num, const QString & material, double thickness, double angle, const QString & DLFSpectra)
    {
        if (!_phyProp)return false;
        if (material == "" || thickness < 0 || num <= 0)return false;
        FITKPhyscisPropertyGeneralLaminate *generalLaminate = dynamic_cast<Acoustics::FITKPhyscisPropertyGeneralLaminate*>(_phyProp);
        if (!generalLaminate)return false;
        int count = generalLaminate->getLayerCount();
        if (count < num) num = count;
        //获取材料ID 并判断材料类型是否是ViscoElastic
        Acoustics::FITKAcousticsPhysics* phy = Acoustics::FITKAcousticsPhysics::GetAcousticsPhysicsFromFrame();
        if (phy == nullptr) return false;
        Acoustics::FITKAcousticsMaterialManager* matMgr = phy->getMaterialManager();
        if (matMgr == nullptr) return false;
        if (!matMgr->getDataByName(material))return false;
        int MaterialID = matMgr->getDataByName(material)->getDataObjectID();
        Acoustics::FITKAcousticsAbsMaterial* materialPtr = dynamic_cast<Acoustics::FITKAcousticsAbsMaterial*>(matMgr->getDataByName(material));
        bool isViscoElastic = materialPtr->getAcousticsMaterialType() == Acoustics::FITKAcousticsAbsMaterial::ViscoElastic;

        //获取频谱ID  
        Acoustics::FITKAcousticsSpectraManager* SpeMgr = phy->getFITKAcousticsSpectraManager();
        if (SpeMgr == nullptr) return false;
        int SplectrumID = -1;
        if (!SpeMgr->getDataByName(DLFSpectra)) {
            if (DLFSpectra != "")return false;
            if (!isViscoElastic)return false;
        }
        else {
            SplectrumID = SpeMgr->getDataByName(DLFSpectra)->getDataObjectID();
        }

        //创建新层
        FITKPhyscisPropertyGeneralLaminateIsoLayer* layer = new FITKPhyscisPropertyGeneralLaminateIsoLayer();
        layer->setMaterialID(MaterialID);
        layer->setThickness(thickness);
        layer->setAngle(angle);
        if (SplectrumID != -1)  layer->setDLFSplectrumID(SplectrumID);

        //插入层
        generalLaminate->insertDataObj(num - 1, layer);
        return true;
    }

    bool PyFITKPhyscisPropGeneralLaminate::deleteLayer(int num)
    {
        if (!_phyProp || num <= 0)return false;
        FITKPhyscisPropertyGeneralLaminate *generalLaminate = dynamic_cast<Acoustics::FITKPhyscisPropertyGeneralLaminate*>(_phyProp);
        if (!generalLaminate)return false;
        int count = generalLaminate->getLayerCount();
        if (count < num)return false;

        generalLaminate->removeDataByIndex(num - 1);
        return true;
    }

    int PyFITKPhyscisPropGeneralLaminate::getLayerCount()
    {
        if (!_phyProp)return -1;
        FITKPhyscisPropertyGeneralLaminate *generalLaminate = dynamic_cast<Acoustics::FITKPhyscisPropertyGeneralLaminate*>(_phyProp);
        if (!generalLaminate)return -1;

        return generalLaminate->getLayerCount();
    }

    bool PyFITKPhyscisPropGeneralLaminate::createPhyscisProp(const QString & name)
    {
        if (name == "")return false;
        Acoustics::FITKAcousticsPhysics* phy = Acoustics::FITKAcousticsPhysics::GetAcousticsPhysicsFromFrame();
        if (phy == nullptr) return false;
        Acoustics::FITKAcousticsPhysicsPropManager* propMgr = phy->getPhysicsPropManager();
        if (propMgr == nullptr) return false;
        if (propMgr->getDataByName(name))return false;

        FITKPhyscisPropertyGeneralLaminate * generalLaminate = new FITKPhyscisPropertyGeneralLaminate();
        generalLaminate->setDataObjectName(name);
        _phyProp = generalLaminate;
        propMgr->appendDataObj(_phyProp);
        return true;
    }
}